package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"path/filepath"
	"runtime"
	"strings"
	"swoole-cli/goos"
	"syscall"
)

func main() {

	//控制
	//ctx, cancel := context.WithCancel(context.Background())

	//监听系统信号
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-signalChan
		fmt.Println("signal received")
		//cancel()
	}()

	binPath, err := goos.GetDefaultGoodsBin().GetCliBin()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(binPath)
	executable := filepath.Dir(filepath.Dir(filepath.Dir(binPath)))
	executable = filepath.ToSlash(executable)
	if runtime.GOOS == "windows" {
		executable = strings.Replace(executable, ":", "", 1)
		executable = "/cygdrive/" + executable
	}
	fmt.Println(executable)
	//argsArr := make([]string, 0)
	//argsArr = append(argsArr, executable+`/composer/composer.phar`)
	//执行命令
	//command := execute.New(ctx, binPath, append(argsArr, os.Args[1:]...)...)
	env := os.Environ()
	procAttr := &os.ProcAttr{
		Env: env,
		Files: []*os.File{
			os.Stdin,
			os.Stdout,
			os.Stderr,
		},
	}

	process, err := os.StartProcess(binPath, []string{"-c", executable + "/php.ini", executable + "/bin/hyperf.php", "start"}, procAttr)
	fmt.Println(process)
	if err != nil {
		log.Fatal(err)
	}
	_, err = process.Wait()
	if err != nil {
		log.Fatal(err)
	}
	//command := execute.New(ctx, binPath, "-c", "./php.ini", "./bin/hyperf.php", "start")

	//command.Run()
}
